import UIKit

//自定义一个类及其子类
class Car{
    var name:String = "汽车"
}

class Audi:Car{
    var brand:String = "奥迪"
}

class BMW:Car{
    var brand:String = "宝马"
}

//创建实例
var car = Car()
var audi = Audi()
var bmw = BMW()

//存放在集合中
var array:[Car] = [car,audi,bmw]

//遍历集合
array.forEach { (car) in
    if car is Audi {
        print("这是辆\((car as! Audi).brand)")
    }else if car is BMW {
        print("这是辆\((car as! BMW).brand)")
    }else {
        print("这是辆\(car.name)")
    }
}


//定义泛型,交换两个参数的值
func exchange<T>(param1 : inout T , param2: inout T){
    let tmp = param1
    param1 = param2
    param2 = tmp
}

var p1 = "hello"
var p2 = "world"
exchange(param1: &p1, param2: &p2)

print("\(p1),\(p2)")


//创建一个类
class MyClass{
    var name:String
    var age:Int
    
    init(name:String,age:Int) {
        self.name = name
        self.age = age
    }
}

//为MyClass扩展一个计算属性
extension MyClass{
    var nameAndAge:String{
        return "我叫\(name),今年\(age)岁了"
    }
}

var my = MyClass(name: "Zhang", age: 18)
print(my.nameAndAge)

protocol Test{
    //可读的
    var name:String{get}
    //可读可写的
    var age:Int{get set}
}

//创建一个类来遵守Test协议
class ClassNew:Test{
    
    var name: String
    
    var age: Int
    
    init(){
        name = "Zhang"
        age = 18
    }
}

var obj = ClassNew()



